package com.zeta.eduservice.service.impl;

import com.zeta.eduservice.entity.question.ChapterAQuestionTypeSysAndChoicequestion;
import com.zeta.eduservice.entity.question.ChapterAQuestionTypeSysAndQuestion;
import com.zeta.eduservice.entity.question.EduChapter;
import com.zeta.eduservice.entity.question.EduChapterAndQuestionTypeSys;
import com.zeta.eduservice.entity.question.EduPaper;
import com.zeta.eduservice.entity.question.EduQuestionTypeSys;
import com.zeta.eduservice.entity.question.EduVideo;
import com.zeta.eduservice.entity.vo.ChapterAndQuestionTypeSysVo;
import com.zeta.eduservice.entity.vo.ChapterVo;
import com.zeta.eduservice.entity.vo.VideoVo;
import com.zeta.eduservice.entity.vo.paperVoback;
import com.zeta.eduservice.entity.vo.node.TypesysNode;
import com.zeta.eduservice.entity.vo.node.chapterNode;
import com.zeta.eduservice.mapper.EduChapterMapper;
import com.zeta.eduservice.service.ChapterAQuestionTypeSysAndChoicequestionService;
import com.zeta.eduservice.service.ChapterAQuestionTypeSysAndQuestionService;
import com.zeta.eduservice.service.EduChapterAndQuestionTypeSysService;
import com.zeta.eduservice.service.EduChapterService;
import com.zeta.eduservice.service.EduPaperService;
import com.zeta.eduservice.service.EduQuestionTypeSysService;
import com.zeta.eduservice.service.EduVideoService;
import com.zeta.exceptionhandler.GuliException;

import net.bytebuddy.agent.builder.AgentBuilder.RedefinitionStrategy.ResubmissionScheduler;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections4.map.HashedMap;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author zeta
 * @param <E>
 * @since 2020-08-02
 */
@Service
public class EduChapterServiceImpl<E> extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

	@Autowired
	private EduQuestionTypeSysService QuestionTypeSysService;
	@Autowired
	private EduChapterAndQuestionTypeSysService ChapterAndQuestionTypeSysService;

//	@Autowired
//	private EduVideoService videoService;
//	@Autowired
//	private EduPaperService paperService;
	@Override
	public List<ChapterVo> nestedList(String courseId) {
		// 最终要的到的数据列表
		ArrayList<ChapterVo> chapterVoArrayList = new ArrayList<>();
		// 获取章节信息
		QueryWrapper<EduChapter> queryWrapper1 = new QueryWrapper<>();

		queryWrapper1.eq("course_id", courseId);
		// queryWrapper1.orderByAsc("sort", "id");
		List<EduChapter> chapters = baseMapper.selectList(queryWrapper1);

		QueryWrapper<EduChapterAndQuestionTypeSys> queryWrapper2 = new QueryWrapper<>();
		queryWrapper2.eq("course_id", courseId);

		List<EduChapterAndQuestionTypeSys> list = ChapterAndQuestionTypeSysService.list(queryWrapper2);
		List<EduQuestionTypeSys> QuestionTypeSysList = QuestionTypeSysService.list(null);
		HashedMap<String, EduQuestionTypeSys> hashedMap = new HashedMap<String, EduQuestionTypeSys>();
		for (EduQuestionTypeSys i : QuestionTypeSysList) {
			hashedMap.put(i.getId(), i);
		}
		// 获取课时信息
		// QueryWrapper<EduVideo> queryWrapper2 = new QueryWrapper<>();
//				queryWrapper2.eq("course_id", courseId);
//				queryWrapper2.orderByAsc("sort", "id");
//				List<EduVideo> videos = videoService.list(queryWrapper2);
//				
//				QueryWrapper<EduPaper> querywrapper3=new QueryWrapper<EduPaper>();
//				querywrapper3.eq("course_id", courseId);
//			
//				List<EduPaper> papers=paperService.list(querywrapper3);
//				//填充章节vo数据
		int count1 = chapters.size();
		for (int i = 0; i < count1; i++) {
			EduChapter chapter = chapters.get(i);

			// 创建章节vo对象
			ChapterVo chapterVo = new ChapterVo();
			if (chapter != null) {
				BeanUtils.copyProperties(chapter, chapterVo);
				chapterVoArrayList.add(chapterVo);

			}

			ArrayList<ChapterAndQuestionTypeSysVo> ChapterAndQuestionTypeSysVoList = new ArrayList<>();
			int count2 = list.size();
			for (int j = 0; j < count2; j++) {

				EduChapterAndQuestionTypeSys video = list.get(j);
				ChapterAndQuestionTypeSysVo videoVo = new ChapterAndQuestionTypeSysVo();
				if (chapter.getId().equals(video.getChapterId())) {

					// 创建课时vo对象

					if (video != null) {

						BeanUtils.copyProperties(video, videoVo);

						videoVo.setQuestionTypeSysName(hashedMap.get(videoVo.getQuestionTypeSysId()).getName());

						ChapterAndQuestionTypeSysVoList.add(videoVo);
					}

				}
//						ArrayList<paperVoback> paperVolist=new ArrayList<paperVoback>();
//						int count3=papers.size();
//						for(int z=0;z<count3;z++) {
//							EduPaper eduPaper = papers.get(z);
//							
//							if(video.getId().equals(eduPaper.getVideoId())){
//								System.out.println(video.toString());
//								paperVoback paperback=new paperVoback();
//								if(paperback!=null) {
//									BeanUtils.copyProperties(eduPaper, paperback);
//									paperVolist.add(paperback);
//								}
//							}
//						}
//						videoVo.setChildren(paperVolist);
			}
			chapterVo.setChildren(ChapterAndQuestionTypeSysVoList);
		}
		return chapterVoArrayList;
	}

	@Override
	public boolean deleteChapter(String chapterId) {
		// QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
		// queryWrapper.eq("chapter_id", chapterId);
		// Integer count = videoService.count(queryWrapper);

		// if(count>0) {
		// throw new GuliException(20001,"删除项有子项，不能删除");
		// }else {
		int result = baseMapper.deleteById(chapterId);
		return result > 0;
		// }

	}

	@Override
	public List<ChapterVo> getChapterVideoByCourseId(String courseId) {

		// 1 根据课程id查询课程里面所有的章节
		QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
		wrapperChapter.eq("course_id", courseId);
		List<EduChapter> eduChapterList = baseMapper.selectList(wrapperChapter);

		// 2 根据课程id查询课程里面所有的小节
		// QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
		// wrapperVideo.eq("course_id",courseId);
		// List<EduVideo> eduVideoList = videoService.list(wrapperVideo);

		// 创建list集合，用于最终封装数据
		List<ChapterVo> finalList = new ArrayList<>();

		// 3 遍历查询章节list集合进行封装
		// 遍历查询章节list集合
		for (int i = 0; i < eduChapterList.size(); i++) {
			// 每个章节
			EduChapter eduChapter = eduChapterList.get(i);
			// eduChapter对象值复制到ChapterVo里面
			ChapterVo chapterVo = new ChapterVo();
			BeanUtils.copyProperties(eduChapter, chapterVo);
			// 把chapterVo放到最终list集合
			finalList.add(chapterVo);

			// 创建集合，用于封装章节的小节
			// List<VideoVo> videoList = new ArrayList<>();

			// 4 遍历查询小节list集合，进行封装
//		            for (int m = 0; m < eduVideoList.size(); m++) {
//		                //得到每个小节
//		                EduVideo eduVideo = eduVideoList.get(m);
//		                //判断：小节里面chapterid和章节里面id是否一样
//		                if(eduVideo.getChapterId().equals(eduChapter.getId())) {
//		                    //进行封装
//		                    VideoVo videoVo = new VideoVo();
//		                    BeanUtils.copyProperties(eduVideo,videoVo);
//		                    //放到小节封装集合
//		                    videoList.add(videoVo);
//		                }
//		            }
			// 把封装之后小节list集合，放到章节对象里面
			// chapterVo.setChildren(videoList);
		}
		return finalList;
	}

	@Autowired
	ChapterAQuestionTypeSysAndQuestionService chapterAQuestionTypeSysAndQuestionService;
	@Autowired
	ChapterAQuestionTypeSysAndChoicequestionService chapterAQuestionTypeSysAndChoicequestionService;
@Autowired
EduQuestionTypeSysService eduQuestionTypeSysService;
	@Override
	public ArrayList<chapterNode> getChapterAndInfo(String courseId) {
		QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<EduChapter>();
		queryWrapper.eq("course_id", courseId);
		List<EduChapter> selectList = baseMapper.selectList(queryWrapper);
		ArrayList<chapterNode> arrayList = new ArrayList<chapterNode>();
		List<EduQuestionTypeSys> list3 = eduQuestionTypeSysService.list(null);
		for(int i=0;i<selectList.size();i++) {
			chapterNode chapterNode = new chapterNode();
			chapterNode.setChapter(selectList.get(i));
			ArrayList<TypesysNode> list=new ArrayList<TypesysNode>();
			List<EduChapterAndQuestionTypeSys> list2 = ChapterAndQuestionTypeSysService.list(new QueryWrapper<EduChapterAndQuestionTypeSys>().eq("chapter_id", selectList.get(i)));
			for(int j=0;j<list2.size();j++) {
				TypesysNode typesysNode = new TypesysNode();
				int count = 0;
				typesysNode.setId(list2.get(j).getId());
				for(int z=0;z<list3.size();z++) {
					if(list3.get(z).getId().equals(list2.get(j).getQuestionTypeSysId())){
						typesysNode.setSys(list3.get(z));
						break;
					}
				}
				
				if(list2.get(j).getQuestionTypeSysId().equals("1")) {
					count=chapterAQuestionTypeSysAndChoicequestionService.count(new QueryWrapper<ChapterAQuestionTypeSysAndChoicequestion>().eq("regist_id", list2.get(j).getId()));
					
				}else {
					count=chapterAQuestionTypeSysAndQuestionService.count(new QueryWrapper<ChapterAQuestionTypeSysAndQuestion>().eq("regist_id", list2.get(j).getId()));
					
				}
				typesysNode.setNum(count);
			}
			chapterNode.setList(list);
			arrayList.add(chapterNode);
			
			
			
		}
	
		return arrayList;
	}
}
